473,440 Members | 1,686 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

TkInter buttons - callback (command) function

It seems that in some cases the callback (command) function in a TkInter Button runs automatically when drawn for the first time. Example:
b=Button(frame, text="Click me",command=myFunction())
b.pack()
..
so, myFunction() executes when the screen is drawn for the first time.
But (!) if I do not include the "()" after myFunction, then it does not run until it is explicitly clicked. IE:
b=Button(frame, text="Click me", command=myFunction)
..
In this case myFunction does not run until clicked.
Is this correct behavior? It seems strange to me, and I do not find it documented anywhere.
(I am using a very recent version of python/tkinter on XP)
Thanks!
Erasmus
Sep 11 '07 #1
2 6633
oops. I see whats going on .. obviously: Python calls the function and returns the result to command. So is there any way to send parameters directly in a callback function?

It seems that in some cases the callback (command) function in a TkInter Button runs automatically when drawn for the first time. Example:
b=Button(frame, text="Click me",command=myFunction())
b.pack()
..
so, myFunction() executes when the screen is drawn for the first time.
But (!) if I do not include the "()" after myFunction, then it does not run until it is explicitly clicked. IE:
b=Button(frame, text="Click me", command=myFunction)
..
In this case myFunction does not run until clicked.
Is this correct behavior? It seems strange to me, and I do not find it documented anywhere.
(I am using a very recent version of python/tkinter on XP)
Thanks!
Erasmus
Sep 11 '07 #2
ilikepython
844 Expert 512MB
oops. I see whats going on .. obviously: Python calls the function and returns the result to command. So is there any way to send parameters directly in a callback function?
Do you mean like this:?
Expand|Select|Wrap|Line Numbers
  1. but = Button(text = "Press", command = lambda x = "blah": myFunction(x))
  2.  
Notice that the function doesn't get called right away, it is in the body of the lambda.
Sep 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Koenig | last post by:
from Tkinter import * class Application(Frame): def setcolor(self): self = "blue" def createWidgets(self): self.b1 = Button(self, bg = "red", command = self.setcolor) self.b1.place(height...
2
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
8
by: dakman | last post by:
Recently, I have been needing to do this alot and I can never find a way around it, the main reason I want to do this is because for example in the application I am making right now, it creates a...
3
by: Jay | last post by:
I wold like to be able to generate buttons from a list in a file. How would I go about making each button have a different command, Lets say make the button print its label instead of print "." ...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
6
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance ...
2
by: Doran, Harold | last post by:
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc was published in 1999 and I wonder if there is a more recent version. I've googled a bit and this version is the one I keep...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.